package com.wanhe.shoppingcart;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wanhe.base.util.NetUtil;
import com.wanhe.base.util.SystemUtil;


/**
 * 自定义对话框
 *
 * @author niwenchi
 */
public class OrderDialog1 extends Dialog implements OnClickListener, DialogInterface.OnDismissListener {

    private Context context;
    private EditText et_job_number;
    private EditText et_table_num;
    private EditText et_count;
    private EditText et_pwd;
    private TextView tvCancle;
    private TextView tvConfirm;
    private OrderPresenter mOrderPresenter;

    public OrderDialog1(Context context, OrderPresenter orderPresenter) {
        super(context, R.style.dialog);
        this.context = context;
        mOrderPresenter = orderPresenter;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).inflate(R.layout.order_dialog, null);
        setContentView(view);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        et_job_number = (EditText) findViewById(R.id.et_job_number);
        et_table_num = (EditText) findViewById(R.id.et_table_num);
        et_count = (EditText) findViewById(R.id.et_count);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        tvCancle = (TextView) findViewById(R.id.btnCancle);
        tvConfirm = (TextView) findViewById(R.id.btnConfrim);

        et_pwd.requestFocus();
        tvCancle.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);

//        et_job_number.setText(WaiterManager.getInstance().getWaiter().getAccount());
//        et_table_num.setText(TableInfoHolder.getInstance().getTableNum());
//        et_count.setText(String.valueOf(TableInfoHolder.getInstance().getCustomNum()));

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id == R.id.btnCancle){//取消
            dismiss();
        }else if(id == R.id.btnConfrim){//确定，下单
            palceOrder();
        }
    }

    /**
     * 下单
     */
    private void palceOrder() {
        if(!NetUtil.hasNetwork(context))return;
        String jobNumber = et_job_number.getText().toString().trim();
        if (null == jobNumber || "".equals(jobNumber)) {
            Toast.makeText(context, "请输入员工号，亲!", Toast.LENGTH_SHORT).show();
            return;
        }

        String tableNum = et_table_num.getText().toString().trim();
        if (null == tableNum || "".equals(tableNum)) {
            Toast.makeText(context, "请输入餐桌号，亲!", Toast.LENGTH_SHORT).show();
            return;
        }

        String count = et_count.getText().toString().trim();
        if (null == count || "".equals(count)) {
            Toast.makeText(context, "请输入用餐人数，亲!", Toast.LENGTH_SHORT).show();
            return;
        }

        String pwd = et_pwd.getText().toString();
        if (null == pwd || "".equals(pwd)) {
            Toast.makeText(context, "请输入密码，亲!", Toast.LENGTH_SHORT).show();
            return;
        }
        mOrderPresenter.placeOrder(jobNumber, tableNum, count, pwd);
        dismiss();

    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        SystemUtil.hideSystemKeyBoard(context, et_pwd);
    }

}
